Es keyword 和 text 类型实验🧪

您所在的位置:网站首页 es fields keyword Es keyword 和 text 类型实验🧪

Es keyword 和 text 类型实验🧪

#Es keyword 和 text 类型实验🧪| 来源: 网络整理| 查看: 265

首先创建一个 es mapping 模版

PUT /_template/test_type_template { "index_patterns": [ "test_type*" // 1 ], "mappings":{ "dynamic":"false", "properties":{ "name":{ "type":"keyword" // 2 }, "city":{ "type":"text" // 3 } } }, "settings":{ "index":{ "number_of_shards":1, //4 代表分片数 "number_of_replicas":1 //5 代表副本数 } } }

解释:

1 以 test_type 为前缀的索引都会按照模版的类型存储数据4 可根据数据量大小设置,数据量比较大时,分片个数就要设置大一点 5 副本分片的目的主要是进行故障转移

然后创建一个 v1 版本的索引,并设置别名 test_type

PUT /test_type_v1 POST /_aliases { "actions" : [ { "add" : { "index" : "test_type_v1", "alias" : "test_type" } } ] }

接着我们插入两条数据

PUT /test_type/_doc/1 { "name" : "叶子在这儿", "city" : "陕西省西安市长安区" } PUT /test_type/_doc/2 { "name":"北京的小家", "city":"北京市昌平区回龙观街道" }

然后我们来做查询实验

注意:name类型:keyword;city类型:textterm 严格匹配查询GET test_type/_search { "query": { "term": { "name": { "value": "叶子在这儿" // 查询 name,可查到结果 } } } } GET test_type/_search { "query": { "term": { "city": { "value": "陕西省西安市长安区" // 查询 city,无结果 } } } } 从实验可看出:keyword 类型可以查到,text 类型查不到wildcard 模糊查询,类似 sql like 查询 GET test_type/_search { "from":0, "size":50, "query":{ "bool":{ "must":{ "wildcard":{ "city":"*北京*" // city查询,无结果 } } } } } GET test_type/_search { "from":0, "size":50, "query":{ "bool":{ "must":{ "wildcard":{ "name":"*叶子*" // name查询,有结果 } } } } } 从实验可看出:keyword 类型可以查到,text 类型查不到怀着好奇,我在网上搜了 keyword 和 text 的区别Text vs. keywordText:会分词,然后进行索引 支持模糊、精确查询 不支持聚合keyword:不进行分词,直接索引 支持模糊、精确查询 支持聚合

小伙伴,你知道为什么 term 和 wildcard 查询,都只有 keyword 类型的字符串可以查到的原因来吗?



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3